<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="缩略图" width="80">
        <template slot-scope="scope">
          <img :src="scope.row.thumb[0] ? scope.row.thumb[0].url : ''" width="80">
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="100" />
      <el-table-column prop="price" label="价格" width="180" />
      <el-table-column prop="priority" label="序号(从小到大)" />
      <el-table-column prop="status" label="推荐" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isBanner" type="primary" close-transition>海报推荐</el-tag>
          <el-tag v-if="scope.row.isRecommend" type="success" close-transition>热门推荐</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="category.title" label="分类" />
      <el-table-column prop="createdAt" label="创建日期" :formatter="dateFormat" />
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button-group>
            <el-button type="primary" size="small" icon="edit" @click.native.prevent="editRow(scope.$index, tableData)">编辑</el-button>
            <el-button type="danger" size="small" icon="delete" @click.native.prevent="deleteRow(scope.$index, tableData)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="pagination" :page-size="pageSize" layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" />
  </div>
</template>
<script>
import moment from '../../utils/moment'
import * as goods from '@/api/goods'
export default {
  data() {
    return {
      tableData: [],
      pageIndex: 0,
      pageSize: require('../../utils/config').pageSize,
      total: 0
    }
  },
  created: function() {
    this.loadData()
    this.countItems()
  },
  methods: {
    loadData: function() {
      goods.list({
        pagination: {
          skip: this.pageIndex * this.pageSize,
          limit: this.pageSize
        }
      }).then(response => {
        console.log(response)
        this.tableData = response.data
      })
    },
    dateFormat: function(row, column) {
      return moment.date(row[column.property])
    },
    deleteRow(index, rows) {
      // 删除确认
      this.$confirm('此操作将永久删除商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 取出当前商品对象
        var goods = this.tableData[index]
        console.log(goods)
        goods.del(goods).then(response => {
          // console.log(response);
          rows.splice(index, 1)
          this.$message({
            type: response.code === 200 ? 'success' : 'error',
            message: response.msg
          })
        })
      }).catch(() => {})
    },
    editRow: function(index, rows) {
      // 取出当前商品对象
      var goods = this.tableData[index]
      this.$router.push({
        name: 'goodsAdd',
        query: {
          _id: goods._id
        }
      })
    },
    handleCurrentChange: function(pageIndex) {
      console.log(pageIndex)
      this.pageIndex = --pageIndex
      this.loadData()
    },
    countItems: function() {
      goods.count().then(response => {
        console.log(response)
        this.total = response.data.count
      })
    }
  }
}
</script>
